﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PMCT
{
    public partial class MgmtLib : Form
    {
        public ConfigRoot curRoot;

        public MgmtLib()
        {
            InitializeComponent();
        }

        private void MgmtLib_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns.Clear();

            DataGridViewColumn ch2 = new DataGridViewTextBoxColumn();
            ch2.Name = "名称";
            dataGridView1.Columns.Add(ch2);

            DataGridViewColumn ch3 = new DataGridViewLinkColumn();
            ch3.Name = "文件名";
            dataGridView1.Columns.Add(ch3);

            DoRefresh();
        }

        private void import_Click(object sender, EventArgs e)
        {
            try
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                DialogResult dr = fbd.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    string[] docFiles = Directory.GetFiles(fbd.SelectedPath, "*.doc", SearchOption.AllDirectories);
                    AddToListUnit(docFiles);

                    string[] xlsFiles = Directory.GetFiles(fbd.SelectedPath, "*.xls", SearchOption.AllDirectories);
                    AddToListUnit(xlsFiles);

                    string[] mppFiles = Directory.GetFiles(fbd.SelectedPath, "*.mpp", SearchOption.AllDirectories);
                    AddToListUnit(mppFiles);

                    DoRefresh();
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("导入异常: " + ex.Message);
            }
        }

        private void AddToListUnit(string[] names)
        {
            foreach (string n in names)
            {
                string fn = Path.GetFileName(n);
                fn = fn.TrimStart(("0123456789.").ToCharArray());
                fn = fn.Replace(Path.GetExtension(n), "");

                string nn = n;
                string rel = Path.GetDirectoryName(Application.ExecutablePath);
                if (nn.StartsWith(rel, StringComparison.OrdinalIgnoreCase) == true)
                {
                    nn = nn.Substring(rel.Length);
                    nn = ".\\" + nn;
                }

                curRoot.listUnit.Add(new Unit(fn, nn));
            }
        }

        private bool bUpdating = false;
        private void DoRefresh()
        {
            bUpdating = true;

            dataGridView1.Rows.Clear();
            if (curRoot.listUnit.Count == 0)
                return;

            dataGridView1.Rows.Add(curRoot.listUnit.Count);

            int cnt = 0;
            foreach (Unit u in curRoot.listUnit)
            {
                int col = 0;
                dataGridView1.Rows[cnt].Cells[col++].Value = u.name;
                dataGridView1.Rows[cnt].Cells[col].Value = Path.GetFileName(u.uri);
                dataGridView1.Rows[cnt].Cells[col++].ToolTipText = u.uri;

                cnt++;
            }

            foreach (DataGridViewColumn col in dataGridView1.Columns)
                col.Width = 200;//col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

            //dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            bUpdating = false;
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string name = (string)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
            string uri = (string)dataGridView1.Rows[e.RowIndex].Cells[1].ToolTipText;
            if (name == null || name == "")
                return;

            if (e.RowIndex >= curRoot.listUnit.Count)
            {
                curRoot.listUnit.Add(new Unit(name, uri));
            }
            else
            {
                curRoot.listUnit[e.RowIndex].name = name;
                curRoot.listUnit[e.RowIndex].uri = uri;
            }
        }

        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            try
            {
                if (bUpdating == true)
                    return;

                curRoot.listUnit.RemoveRange(e.RowIndex, e.RowCount);
            }
            catch (Exception) { }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex < 0)
                return;

            if (e.Button == MouseButtons.Right)
            {
                if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewLinkColumn)
                {
                    try
                    {
                        OpenFileDialog ofd = new OpenFileDialog();
                        ofd.Filter = "Word files (*.doc)|*.doc|Excel files (*.xls)|*.xls|All files (*.*)|*.*";
                        ofd.Title = "选择目标文件";
                        ofd.Multiselect = false;
                        DialogResult dr = ofd.ShowDialog();
                        if (dr == DialogResult.OK)
                        {
                            string path = ofd.FileName;
                            string rel = Path.GetDirectoryName(Application.ExecutablePath);
                            if (path.StartsWith(rel, StringComparison.OrdinalIgnoreCase) == true)
                            {
                                path = path.Substring(rel.Length);
                                path = ".\\" + path;
                            }
                            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = path;
                            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = ofd.SafeFileName;

                            curRoot.listUnit[e.RowIndex].uri = path;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show("打开文件异常: " + ex.Message);
                    }
                }
            }
            else if (e.Button == MouseButtons.Left)
            {
                if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewLinkColumn)
                {
                    string uri = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText;
                    if (uri.StartsWith(".\\"))
                    {
                        string rel = Path.GetDirectoryName(Application.ExecutablePath);
                        uri = rel + uri.Substring(2);
                    }
                    if (File.Exists(uri))
                    {
                        System.Diagnostics.Process.Start(uri);
                    }
                }
            }
        }
    }
}
